/**
 * 
 */
package com.zmht.specedu.senate.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.connection.ConnectionProvider;
import org.hibernate.engine.SessionFactoryImplementor;

import com.zmht.specedu.frame.dao.impl.GenericDao;
import com.zmht.specedu.frame.model.PageInfoModel;
import com.zmht.specedu.frame.util.StringUtils;
import com.zmht.specedu.senate.dao.IClassDao;
import com.zmht.specedu.vo.Bjclass;

/**
 * @author 张天兴 2009年4月22
 * 班级管理
 */
public class ClassDaoImpl extends GenericDao<Bjclass,Integer> implements IClassDao {

	public ClassDaoImpl() {
		super(Bjclass.class);
	}

	
	 

	
	/**
	 * 添加班级时，作出判断，
	 *  不允许添加重复的班级
	 * 
	 * @return
	 * @throws Exception
	 */
	public boolean isValidReg(Integer classType, Integer grade, String classNumber)
			throws Exception {
		boolean flag=false;
		   int length=classNumber.length();
		   List list=this.findByHQL("from Bjclass b where  b.classType='"+classType+"' and b.grades.getId()='"+grade+"' and b.classNumber='"+classNumber+"'");
		return  list.size()==0?true:false;
		
	}

	/**
	 * 班级维护时,根据教师姓名和班级编号,进行查询
	 * @param pageInfo
	 * @param tname
	 * @param classNumber
	 * @return
	 * @throws Exception
	 */
	public PageInfoModel getListByClassNumber(PageInfoModel pageInfo,
			String tname, String classNumber) throws Exception {

		try{		
		 			 	
		 String hql1 ="";
		 if(tname==null||  tname.trim().equals("") || classNumber.equals("所有")){
		 hql1 = "select b from Bjclass b where 1=1 ";
		 }

		 if(StringUtils.isNotEmpty(tname)){
			 hql1 = "select b from Bjclass b left join b.teachers t where t.tname like '%"+tname+"%'";
		 }
		 if(StringUtils.isNotEmpty(classNumber) && !classNumber.equals("所有")){
			 hql1+= " and b.classId="+classNumber;
		 }
		 hql1+=" order by b.addtime desc";
		 pageInfo = this.findByHQL(hql1, pageInfo);
		 System.out.println("pageInfo.getTotalCount()=="+pageInfo.getTotalCount());
		 int num= pageInfo.getTotalCount();
		 if(num>0){
		 for(Object bj : pageInfo.getResultItems()){
			 if(((Bjclass)bj).getTeachers()!=null)
				 ((Bjclass)bj).getTeachers().iterator();
		 }}
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}
		return  pageInfo;
	}
	
	public List getClassNumberList() throws Exception {
		List getNum=this.findByHQL("from Bjclass b order by b.classId desc");
		return getNum;
	}
			

	/**
	 * 通过班级编号,获得班级classId;
	 * 以方便学习矫治使用.
	 * @param classNumber
	 * @return
	 * @throws Exception
	 */
	public Bjclass findClassIdByClassNumber(String classNumber)
			throws Exception {
		
       this.findByHQL("from Bjclass b where b.classNumber='"+classNumber+"'");
		
		return null;
	}

//	/**
//	 * 班级维护时,根据教师姓名和班级编号,进行查询
//	 * @param pageInfo
//	 * @param tname
//	 * @param classNumber
//	 * @return
//	 * @throws Exception
//	 */
//	public PageInfoModel getListByClassNumber(PageInfoModel pageInfo,
//			String tname, String classNumber) throws Exception {
//		    String hql="";
//		try{			  
//		 String hql1 = "from Bjclass b where 1=1 ";
//		 if(StringUtils.isNotEmpty(tname)){
//			 hql1 = "from Bjclass b  inner join b.teachers t where t.tname like '%"+tname+"%'";
//		 }
//		 if(StringUtils.isNotEmpty(classNumber) && !classNumber.equals("所有")){
//			 hql1+= " and b.classNumber="+classNumber;
//		 }
//		 pageInfo = this.findByHQL(hql1, pageInfo);
//		 for(Object bj : pageInfo.getResultItems()){
//			 if(((Bjclass)bj).getTeachers()!=null)
//				 ((Bjclass)bj).getTeachers().iterator();
//		 }
//		}catch(Exception e){
//			e.printStackTrace();
//			throw e;
//		}
//		return  pageInfo;
//	}
		

}
